(in-package #:nyxt-user)
(ql:quickload 'sera)
(ql:quickload 'spinneret)

(defun list-modes (buffer)
  "List all the enabled modes of the given buffer"
  (format nil "~{~a~^ ~}" (mapcar #'format-mode (modes buffer))))

(defun format-status-modes (buffer window)
  "I'm not sure what this is?"
  (spinneret:with-html-string
    (when (nosave-buffer-p buffer) (:span "履Incogneto")) ; If you're in a nosave buffer display it
   
    (:a :class "button"
        :href (lisp-url '(nyxt:toggle-modes))
        :title (str:concat "Enabled modes: " (list-modes buffer)) "✚") ; Where is this displaying?
    
    (loop for mode in 
        (sera:filter #'visible-in-status-p (modes buffer)) ; for modes in buffer?
        collect
        (let* 
            (
               (formatted-mode 
                 (if (glyph-mode-presentation-p (status-buffer window))
                     (glyph mode) (format-mode mode)))
               ;; Parsing HTML is a valid way to know if HTML it is.
               (parsed-mode (ignore-errors (plump:parse formatted-mode)))
            )
            (if (and 
                  (sera:single (plump:children parsed-mode))
                  (plump:text-node-p (elt (plump:children parsed-mode) 0)))
                (:a :class "button" 
                 :href (lisp-url `(describe-class ',(mode-name mode)))
                 :title (format nil "Describe ~a" (mode-name mode))
                 formatted-mode) (:raw formatted-mode))))))

(defun format-status-buttons ()
  "Section meant for button controls"
  (spinneret:with-html-string
    (:a :class "button" :title "Backwards" :href (lisp-url '(nyxt/web-mode:history-backwards)) "«")
    (:a :class "button" :title "Reload" :href (lisp-url '(nyxt:reload-current-buffer)) "↺")
    (:a :class "button" :title "Forwards" :href (lisp-url '(nyxt/web-mode:history-forwards)) "»")
    ;(:a :class "button" :title "Execute" :href (lisp-url '(nyxt:execute-command)) "≡"))
  )

(defun format-status-vi-mode (&optional (buffer (current-buffer)))
  (spinneret:with-html-string
    (cond 
      ((find-submode buffer 'vi-normal-mode)
       (:div
         (:a :class "button" 
          :title "command" 
          :href (lisp-url '(nyxt/vi-mode:vi-insert-mode)) "C")))
      ((find-submode buffer 'vi-insert-mode)
       (:div
        (:a :class "button" 
         :title "insert" 
         :href (lisp-url '(nyxt/vi-mode:vi-normal-mode)) "I")))
      (t (:span "")))))

(defun format-status-load-status (buffer)
  (spinneret:with-html-string
    (:div 
     :class (if (and 
                  (web-buffer-p buffer)
                  (eq (slot-value buffer 'load-status) :loading))
                "Loading Page..." ""))))

(defun format-status-url (buffer)
  (spinneret:with-html-string
    (:a :class "button"
        :href (lisp-url '(nyxt:set-url))
        (format nil "~a"
                (render-url (url buffer))
                (title buffer)))))

(defun format-status-tabs ()
  (spinneret:with-html-string
    (loop for domain in 
          ;(remove-duplicates
          ;  (sera:filter-map #'quri:uri-domain (mapcar #'url (sort-by-time (buffer-list))))
          ;  :test #'equal)
          collect (:a :class "tab"
                   :href (lisp-url `(nyxt::switch-buffer-or-query-domain ,domain)) 
                   (format nil "~a" (title domain))))))

(defun format-status (window)
  "Final status function that control the entire bar"
    (let* 
        (
         (buffer (current-buffer window))
         (vi-class (cond ; Find vi mode mode
                     ((find-submode buffer 'vi-normal-mode) "command")
                     ((find-submode buffer 'vi-insert-mode) "insert")))
        )
      
    (spinneret:with-html-string
      (:div :id (if vi-class "container-vi" "container")
       (:td
      
        (:tr 
         (:div :id "tabs"
          (:raw (format-status-tabs))))
        
        (:tr 
         (:div :id "url" :class "rounded"
          (:raw (format-status-load-status buffer) (format-status-url buffer)))) ; <load status> <current url>
        
         (:tr
          (:div :id "buttons" :class "rounded-right" ; Try to make status bar sections rounded
           (:raw (format-status-buttons)))
          (when vi-class
            (:div :id "vi-mode" :class "rounded-right"
             (:raw (format-status-vi-mode buffer)))) ; <current vi mode>
          (:div :id "modes" :class "rounded-left"
           ; :title (list-modes buffer) ; why does this have this?
           (:raw (format-status-modes buffer window)))))))))
